package com.code.service;

import com.code.common.constants.HeaderConstants;
import com.code.common.constants.ServerConstants;
import com.code.common.util.RequestContextUtil;
import com.code.manager.HttpApi;
import com.code.model.ResponseUtil;
import com.code.point.api.PointService;
import com.code.point.model.po.Point;
import com.code.user.api.UserService;
import com.code.user.model.po.User;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import retrofit2.Call;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户业务聚合服务层
 *
 * @author tangyifei
 */
@Service("userClient")
@Slf4j
public class UserClient {

//    @Value("${verify.token.url}")
//    private String verifyTokenUrl;

    @Resource
    private HttpApi httpApi;

    /**
     * 调用userService和pointService失败只重试1次，超过 30000ms调用仍未返回则调用失败
     */
    @Reference(
            version = "${service.version}",
            interfaceClass = UserService.class,
            cluster = "${dubbo.consumer.cluster}",
            timeout = 30000,
            retries = 1
    )
    private UserService userService;

    @Reference(
            version = "${service.version}",
            interfaceClass = PointService.class,
            cluster = "${dubbo.consumer.cluster}",
            timeout = 30000,
            retries = 1
    )
    private PointService pointService;

    //    @Transactional(transactionManager = ServerConstants.TRANSACTION_MANAGER, rollbackFor = Exception.class)
    @GlobalTransactional(timeoutMills = 300000, name = ServerConstants.SEATA_TRANSACTION_APPLICATON_ID)
    public User addUserAndPoint(User user) {
        user.setUserStatus(User.UserStatusEnum.ENABLED.name());
        user.setUserLevel(0);
        user.setUserType(User.UserTypeEnum.NORMAL.name());
        user.setIsAdmin(0);
        userService.insert(user);
        Point point = new Point();
        point.setUserId(user.getId());
        point.setCredit(0);
        pointService.insert(point);
        return user;
    }

    public String getPoint() {
        return pointService.getPoint();
    }

    public String[] getArray() {
        return pointService.getArray();
    }

    public List<String> getList() {
        return pointService.getList();
    }

    public Set<Integer> getSet() {
        return pointService.getSet();
    }

    public Map<String, String> getMap() {
        return pointService.getMap();
    }

    public boolean verifyToken() throws Exception {
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(verifyTokenUrl)
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//        HttpApi httpApi = retrofit.create(HttpApi.class);
        HttpServletRequest request = RequestContextUtil.getRequest();
        String token = request.getHeader(HeaderConstants.X_TOKEN);
        Call<ResponseUtil> call = httpApi.verifyToken(token);
        ResponseUtil body = call.execute().body();
        String rtnCode = body.getCode();
        if ("0".equals(rtnCode)) {
            return true;
        }
        return false;
    }
}
